package com.lucenly.card.fragment;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lucenly.card.R;
import com.lucenly.card.activity.CodeActivity;
import com.lucenly.card.activity.LoginActivity;
import com.lucenly.card.activity.MsgListActivity;
import com.lucenly.card.activity.SettingActivity;
import com.lucenly.card.activity.WebViewActivity;
import com.lucenly.card.base.BaseFragment;
import com.lucenly.card.bean.Banner;
import com.lucenly.card.bean.HomeData;
import com.lucenly.card.bean.Rule;
import com.lucenly.card.bean.UserInfo;
import com.lucenly.card.constants.Constants;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.event.StringMsgEvent;
import com.lucenly.card.utils.AdvancedCountdownTimer;
import com.lucenly.card.utils.BeijingTime;
import com.lucenly.card.utils.DataCleanManager;
import com.lucenly.card.utils.DateUtil;
import com.lucenly.card.utils.GlideCacheUtil;
import com.lucenly.card.utils.GlideUtils;
import com.lucenly.card.utils.PublicUtil;
import com.lucenly.card.utils.TimeUtils;
import com.lucenly.card.view.FocusLabelTextView;
import com.lucenly.card.view.MultiImageView;
import com.lucenly.card.view.PayStatusDialog;
import com.lucenly.card.view.dialog.ClearDialog;
import com.lucenly.card.view.dialog.HomeDialog;
import com.lucenly.card.view.dialog.NoticeDialog;
import com.lucenly.card.view.refresh2.RefreshLayout;
import com.lucenly.card.view.refresh2.SmartRefreshLayout;
import com.lucenly.card.view.refresh2.listener.OnRefreshListener;
import com.siberiadante.toastutils.ToastUtil;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.text.ParseException;

import butterknife.BindView;
import ezy.ui.layout.LoadingLayout;

public class HomeFragment extends BaseFragment{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @BindView(R.id.tv_agree)
    TextView tv_agree;
    @BindView(R.id.tv_totalPeople)
    TextView tv_totalPeople;
    @BindView(R.id.tv_totalFee)
    TextView tv_totalFee;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_punchNum)
    TextView tv_punchNum;
    @BindView(R.id.tv_notPunchNum)
    TextView tv_notPunchNum;
    @BindView(R.id.tv_name_1)
    TextView tv_name_1;
    @BindView(R.id.tv_name_2)
    TextView tv_name_2;
    @BindView(R.id.tv_name_3)
    TextView tv_name_3;
    @BindView(R.id.tv_text_1)
    TextView tv_text_1;
    @BindView(R.id.tv_text_2)
    TextView tv_text_2;
    @BindView(R.id.tv_text_3)
    TextView tv_text_3;
    @BindView(R.id.iv_earliest)
    MultiImageView iv_earliest;
    @BindView(R.id.iv_most)
    MultiImageView iv_most;
    @BindView(R.id.iv_longest)
    MultiImageView iv_longest;
    @BindView(R.id.ll_user)
    LinearLayout ll_user;
    @BindView(R.id.tv_djs)
    TextView tv_djs;//打卡倒计时
    @BindView(R.id.tv_zj)
    TextView tv_zj;//追加挑战金
    @BindView(R.id.ll_status_1)
    LinearLayout ll_status_2;//status = 2时显示
    @BindView(R.id.tv_dk)
    TextView tv_dk;//status=3显示  打卡
    @BindView(R.id.tv_curren_time)
    TextView tv_curren_time;
    @BindView(R.id.tv_tzgz)
    TextView tv_tzgz;
    @BindView(R.id.iv_banner)
    SimpleDraweeView iv_banner;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_gg)
    FocusLabelTextView  tv_gg;
    @BindView(R.id.rl_right)
    RelativeLayout rl_right;
    @BindView(R.id.ll_three)
            LinearLayout ll_three;
    @BindView(R.id.ll_earliest)
            LinearLayout ll_earliest;
    @BindView(R.id.ll_most)
            LinearLayout ll_most;
    @BindView(R.id.ll_longest)
            LinearLayout ll_longest;
    @BindView(R.id.view)
    LoadingLayout view;
    @BindView(R.id.tv_read_num)
    TextView tv_read_num;
    Handler mHandler = new Handler();
    long beijingTime;
    TimeThread timeThread;
    NoticeDialog noticeDialog;
    NoticeDialog msgDialog;
    HomeDialog homeDialog;
    AdvancedCountdownTimer timer;
    boolean isGetData = false;
    boolean isGetTime = false;
    @Override
    protected int getLayoutID() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View contentView) {
        view.showLoading();
        timeThread = new TimeThread();
        tv_title.setText(getResources().getString(R.string.app_name));
        ll_back.setVisibility(View.GONE);
        rl_right.setVisibility(View.VISIBLE);
    }

    @Override
    protected void bindEvent() {
        C(tv_agree);
        C(tv_tzgz);
        C(tv_gg);
        C(rl_right);
        C(tv_zj);
        C(tv_dk);
        C(tv_djs);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
        view.setRetryListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshLayout.autoRefresh();
            }
        });
    }

    @Override
    protected void initData() {

    }
    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    @Subscribe
    public void login(IEvent iEvent){
        if(iEvent!=null && iEvent instanceof StringMsgEvent){
            if(((StringMsgEvent) iEvent).getMsg().equals("login")){
//                getData();
            }else if(((StringMsgEvent) iEvent).getMsg().equals("read")){
                getUnReadCount();
            }else if(((StringMsgEvent) iEvent).getMsg().equals("CHONGZHI")){
//                getData();
//                setStatus(2);
                showDkDialog();
            }
        }
    }
    private void getData() {
        //获取未读数
        getUnReadCount();
        //获取banner
        PublicUtil.getBanner(Constants.BANNER_HOME, new ACallback<Banner>() {
            @Override
            public void onSuccess(final Banner data) {
                iv_banner.setImageURI(data.images);
                iv_banner.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(data==null){
                            return;
                        }
                        //Banner跳转
                        Intent intent = new Intent(mContext, WebViewActivity.class);
                        intent.putExtra("type",1);
                        intent.putExtra("path",data.url);
                        mContext.startActivity(intent);
                    }
                });
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
        //获取规则
        PublicUtil.getRule(Constants.RULE_TZ, new ACallback<Rule>() {
            @Override
            public void onSuccess(Rule data) {
                noticeDialog = new NoticeDialog(getActivity(),data,"确定");
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
        //获取公告
        PublicUtil.getRule(Constants.RULE_HOME, new ACallback<Rule>() {
            @Override
            public void onSuccess(Rule data) {
                tv_gg.setText(data.detail);
                msgDialog = new NoticeDialog(getActivity(),data,"朕知道了");
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
        ViseHttp.GET(UrlDefend.HOME_DATA)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<HomeData>() {
                    @Override
                    public void onSuccess(HomeData data) {
                        Log.i("data",data.toString());
                        tv_totalPeople.setText(data.totalPeople+"");
                        tv_totalFee.setText(data.totalFee+"");
                        tv_notPunchNum.setText(data.todayInfo.notPunchNum+"");
                        tv_punchNum.setText(data.todayInfo.punchNum+"");
                        tv_time.setText("打卡时间:  "+data.punchBeginTime+"-"+data.punchEndTime);
                        //获取时间
//                        getTime(data,data.punchBeginTime);
                        getTime(data,data.nowTime*1000,data.punchBeginTime);
                        homeDialog = new HomeDialog(getActivity(),data.punchDeposit);
                        if(data.todayInfo.earliest==null){
                            ll_earliest.setVisibility(View.GONE);
                        }else{
                            GlideUtils.getInstance(mContext).load(mContext,data.todayInfo.earliest.avatar,iv_earliest,R.color.gray_de);
                            tv_name_1.setText(data.todayInfo.earliest.nickname);
                            long time = 0;
                            if((data.todayInfo.earliest.punchTime+"").endsWith("000")){
                                time = data.todayInfo.earliest.punchTime;
                            }else{
                                time = data.todayInfo.earliest.punchTime*1000;
                            }
                            tv_text_1.setText(DateUtil.getMillon(time)+"打卡");
                        }
                        if(data.todayInfo.most==null){
                            ll_most.setVisibility(View.GONE);
                        }else {
                            GlideUtils.getInstance(mContext).load(mContext,data.todayInfo.most.avatar,iv_most,R.color.gray_de);
                            tv_name_2.setText(data.todayInfo.most.nickname);
                            tv_text_2.setText(data.todayInfo.most.rewardFee+"元");
                        }
                        if(data.todayInfo.longest==null){
                            ll_longest.setVisibility(View.GONE);
                        }else{
                            GlideUtils.getInstance(mContext).load(mContext,data.todayInfo.longest.avatar,iv_longest,R.color.gray_de);
                            tv_name_3.setText(data.todayInfo.longest.nickname);
                            tv_text_3.setText("连续"+data.todayInfo.longest.days+"次");
                        }
                        isGetData = true;
                        if(isGetTime&&isGetData){
                            refreshLayout.finishRefresh();
                            view.showContent();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        refreshLayout.finishRefresh();
                        view.setErrorText(errMsg);
                        view.showError();
                    }
                });
        ViseHttp.GET(UrlDefend.HOME_USER)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        refreshLayout.finishRefresh();
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            JSONArray list = jsonObject.getJSONArray("list");
                            if(ll_user.getChildCount()>0){
                                ll_user.removeAllViews();
                            }
                            if(list!=null&&list.length()>0){
                                for(int i = 0 ; i < list.length();i++){
                                    String avatar = list.getJSONObject(i).getString("avatar");
                                    String nickname = list.getJSONObject(i).getString("nickname");
                                    MultiImageView imageView = new MultiImageView(getActivity());
                                    ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                    layoutParams.width = (int) getResources().getDimension(R.dimen.dp_30);
                                    layoutParams.height = (int) getResources().getDimension(R.dimen.dp_30);
                                    imageView.setLayoutParams(layoutParams);
                                    GlideUtils.getInstance(mContext).load(mContext,avatar,imageView,R.color.gray_de);
                                    ll_user.addView(imageView);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        refreshLayout.finishRefresh();
                    }
                });
    }

    private void getUnReadCount() {
        ViseHttp.GET(UrlDefend.UNREAD_COUNT)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            String count = jsonObject.getString("count");
                            if(count.equals("0")){
                                tv_read_num.setVisibility(View.INVISIBLE);
                            }else{
                                tv_read_num.setText(count);
                                tv_read_num.setVisibility(View.VISIBLE);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }
    public void getTime(HomeData data,long beijingTime,final String punchBeginTime){
        this.beijingTime = beijingTime;
        Constants.time = TimeUtils.stampToDate(beijingTime+"","MM月dd日");
        Constants.YEAR = TimeUtils.stampToDate(beijingTime+"","yyyy年");
        Constants.MONTH = TimeUtils.stampToDate(beijingTime+"","MM月");
        final String time = TimeUtils.stampToDate(beijingTime + "", "HH:mm");
//                                String currentTime = TimeUtils.dateToStamp(time,"HH:mm");
        long hour = (beijingTime / (60 * 60 * 1000));//13
        long min = ((beijingTime / (60 * 1000)) - hour * 60);//35
        long sec = (beijingTime/1000-hour*60*60-min*60);//48960-46800-2100
        String[] split = time.split(":");
        int startTime = 6;
        int startMinue = 0;
        if(punchBeginTime.contains(":")){
            String[] split1 = punchBeginTime.split(":");
            startTime = Integer.parseInt(split1[0]);
            startMinue = Integer.parseInt(split1[1]);
        }
        //打卡结束时间
        int endTime = 8;
        int endMinue = 0;
        if(data.punchEndTime.contains(":")){
            String[] split2 = data.punchEndTime.split(":");
            endTime = Integer.parseInt(split2[0]);
            endMinue = Integer.parseInt(split2[1]);
        }
        //现在时间
        String hh = TimeUtils.stampToDate(beijingTime + "", "HH:mm");
        Log.e("hh:",hh+"==="+beijingTime);
        String[] nowTime = hh.split(":");
        long mtTime = 0;
//                            if(i==1){
        //没超过打卡时间
        String s = TimeUtils.stampToDate(beijingTime + "", "yyyy-MM-dd");
        try {
            String toStamp = TimeUtils.dateToStamp(s, "yyyy-MM-dd");
            Log.e("toStamp",toStamp+"");
            mtTime = Long.parseLong(toStamp)+(24 * 60 * 60 * 1000) + startTime * 60 * 60 * 1000 + (startMinue * 60 * 1000) ;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long millisInFuture = mtTime-beijingTime;
        if(data.firstPayTime==0){
            if(millisInFuture>=(24 * 60 * 60 * 1000)){
                millisInFuture-=(24 * 60 * 60 * 1000);
            }
        }else{
            String beiTime = TimeUtils.stampToDate(beijingTime + "", "yyyy-MM-dd");
            String dkTime = TimeUtils.stampToDate(data.firstPayTime*1000 + "", "yyyy-MM-dd");
            Log.e("时间",beiTime+"==="+dkTime+"");
            if(!beiTime.equals(dkTime)){
                millisInFuture-=(24 * 60 * 60 * 1000);
            }
        }
        if(millisInFuture<0){
            millisInFuture+=(24 * 60 * 60 * 1000);
        }
        Log.e("mtTime时间",mtTime+"");
        Log.e("JtTime时间",beijingTime+"");
        Log.e("相差时间",millisInFuture+"");
        long hour1 = (millisInFuture / (60 * 60 * 1000) );//13
        long min1 = ((millisInFuture / (60 * 1000)) - hour1 * 60);//35
        long sec1 = (millisInFuture/1000-hour1*60*60-min1*60);//48960-46800-2100
        tv_djs.setText("打卡倒计时: "+hour1+"时"+min1+"分"+sec1+"秒");
        Log.e("倒计时",hour1+"时"+min1+"分"+sec1+"秒");
        if(timer!=null){
            timer.cancel();
        }
        timer = new AdvancedCountdownTimer(millisInFuture,1000) {
            @Override
            public void onTick(long millisUntilFinished, int percent) {//48960000
                int day=0;
                long hour = (millisUntilFinished / (60 * 60 * 1000) - day * 24);//13
                long min = ((millisUntilFinished / (60 * 1000)) - day * 24 * 60 - hour * 60);//35
                long sec = (millisUntilFinished/1000-day*24*60*60-hour*60*60-min*60);//48960-46800-2100
                tv_djs.setText("打卡倒计时: "+hour+"时"+min+"分"+sec+"秒");
            }

            @Override
            public void onFinish() {
                setStatus(3);
                Log.e("打卡倒计时完成","=============");
                tv_djs.setText("打卡倒计时: "+"23"+"时"+"59"+"分"+"59"+"秒");
                if(timer!=null){
                    timer.cancel();
                }
                timer.setmTotalTime(24*60*60*1000);
                timer.start();
            }
        };
        timer.start();
        tv_curren_time.setText(time);
        if(!timeThread.isAlive()){
            timeThread.start();
        }
        isGetTime = true;
        if(isGetTime&&isGetData){
            refreshLayout.finishRefresh();
            view.showContent();
        }
        setStatus(data.status);
    }
    private void getTime(final HomeData data, final String punchBeginTime) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                beijingTime = BeijingTime.getBeijingTime();
                if(beijingTime!=0){
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Constants.time = TimeUtils.stampToDate(beijingTime+"","MM月dd日");
                            Constants.YEAR = TimeUtils.stampToDate(beijingTime+"","yyyy年");
                            Constants.MONTH = TimeUtils.stampToDate(beijingTime+"","MM月");
                            final String time = TimeUtils.stampToDate(beijingTime + "", "HH:mm");
//                                String currentTime = TimeUtils.dateToStamp(time,"HH:mm");
                            long hour = (beijingTime / (60 * 60 * 1000));//13
                            long min = ((beijingTime / (60 * 1000)) - hour * 60);//35
                            long sec = (beijingTime/1000-hour*60*60-min*60);//48960-46800-2100
                                String[] split = time.split(":");
                            int startTime = 6;
                            int startMinue = 0;
                            if(punchBeginTime.contains(":")){
                                String[] split1 = punchBeginTime.split(":");
                                startTime = Integer.parseInt(split1[0]);
                                startMinue = Integer.parseInt(split1[1]);
                            }
                            //打卡结束时间
                            int endTime = 8;
                            int endMinue = 0;
                            if(data.punchEndTime.contains(":")){
                                String[] split2 = data.punchEndTime.split(":");
                                endTime = Integer.parseInt(split2[0]);
                                endMinue = Integer.parseInt(split2[1]);
                            }
                            //现在时间
                            String hh = TimeUtils.stampToDate(beijingTime + "", "HH:mm");
                            Log.e("hh:",hh+"==="+beijingTime);
                            String[] nowTime = hh.split(":");
                            long mtTime = 0;
//                            if(i==1){
                                //没超过打卡时间
                                String s = TimeUtils.stampToDate(beijingTime + "", "yyyy-MM-dd");
                                try {
                                    String toStamp = TimeUtils.dateToStamp(s, "yyyy-MM-dd");
                                    Log.e("toStamp",toStamp+"");
                                    mtTime = Long.parseLong(toStamp)+(24 * 60 * 60 * 1000) + startTime * 60 * 60 * 1000 + (startMinue * 60 * 1000) ;
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
//                            }else{
//                                String s = TimeUtils.stampToDate(beijingTime + "", "yyyy-MM-dd");
//                                try {
//                                    String toStamp = TimeUtils.dateToStamp(s, "yyyy-MM-dd");
//                                    Log.e("toStamp",toStamp+"");
//                                    mtTime = Long.parseLong(toStamp) + startTime * 60 * 60 * 1000 + (startMinue * 60 * 1000) ;
//                                } catch (ParseException e) {
//                                    e.printStackTrace();
//                                }
//                            }
                            long millisInFuture = mtTime-beijingTime;
                                if(data.firstPayTime==0){
                                    if(millisInFuture>=(24 * 60 * 60 * 1000)){
                                        millisInFuture-=(24 * 60 * 60 * 1000);
                                    }
                                }else{
                                    String beiTime = TimeUtils.stampToDate(beijingTime + "", "yyyy-MM-dd");
                                    String dkTime = TimeUtils.stampToDate(data.firstPayTime*1000 + "", "yyyy-MM-dd");
                                    Log.e("时间",beiTime+"==="+dkTime+"");
                                    if(!beiTime.equals(dkTime)){
                                        millisInFuture-=(24 * 60 * 60 * 1000);
                                    }
                                }
                            if(millisInFuture<0){
                                millisInFuture+=(24 * 60 * 60 * 1000);
                            }
                            Log.e("mtTime时间",mtTime+"");
                            Log.e("JtTime时间",beijingTime+"");
                            Log.e("相差时间",millisInFuture+"");
                            long hour1 = (millisInFuture / (60 * 60 * 1000) );//13
                            long min1 = ((millisInFuture / (60 * 1000)) - hour1 * 60);//35
                            long sec1 = (millisInFuture/1000-hour1*60*60-min1*60);//48960-46800-2100
                            tv_djs.setText("打卡倒计时: "+hour1+"时"+min1+"分"+sec1+"秒");
                            Log.e("倒计时",hour1+"时"+min1+"分"+sec1+"秒");
                            if(timer!=null){
                                timer.cancel();
                            }
                             timer = new AdvancedCountdownTimer(millisInFuture,1000) {
                                @Override
                                public void onTick(long millisUntilFinished, int percent) {//48960000
                                    int day=0;
                                    long hour = (millisUntilFinished / (60 * 60 * 1000) - day * 24);//13
                                    long min = ((millisUntilFinished / (60 * 1000)) - day * 24 * 60 - hour * 60);//35
                                    long sec = (millisUntilFinished/1000-day*24*60*60-hour*60*60-min*60);//48960-46800-2100
                                    tv_djs.setText("打卡倒计时: "+hour+"时"+min+"分"+sec+"秒");
                                }

                                @Override
                                public void onFinish() {
                                    setStatus(3);
                                    Log.e("打卡倒计时完成","=============");
                                    tv_djs.setText("打卡倒计时: "+"23"+"时"+"59"+"分"+"59"+"秒");
                                    if(timer!=null){
                                        timer.cancel();
                                    }
                                    timer.setmTotalTime(24*60*60*1000);
                                    timer.start();
                                }
                            };
                            timer.start();
                            tv_curren_time.setText(time);
                            if(!timeThread.isAlive()){
                                timeThread.start();
                            }
                            isGetTime = true;
                            if(isGetTime&&isGetData){
                                refreshLayout.finishRefresh();
                                view.showContent();
                            }
                            setStatus(data.status);
                        }
                    });
                }else{
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            view.setErrorText("时间获取失败");
                            view.showError();
                        }
                    });
                }
            }
        }).start();
    }
    public class TimeThread extends Thread {
        @Override
        public void run () {
            do {
                try {
                    Thread.sleep(1000);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            beijingTime+=1000;
                            final String time = TimeUtils.stampToDate(beijingTime + "", "HH:mm");
                            tv_curren_time.setText(time);
                        }
                    });
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while(true);
        }
    }
    /**
     * 根据状态显示首页按钮
     * @param status
     */
    private void setStatus(int status) {
        switch (status){
            case 1:
                tv_agree.setVisibility(View.VISIBLE);
                ll_status_2.setVisibility(View.GONE);
                tv_dk.setVisibility(View.GONE);
                break;
            case 2:
                tv_agree.setVisibility(View.GONE);
                ll_status_2.setVisibility(View.VISIBLE);
                tv_dk.setVisibility(View.GONE);
                break;
            case 3:
                tv_agree.setVisibility(View.GONE);
                ll_status_2.setVisibility(View.GONE);
                tv_dk.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void processClick(View view) {
        switch (view.getId()){
            case R.id.tv_agree:
                UserInfo user = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
                if(user==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }else{
                    if(homeDialog!=null){
                        homeDialog.show();
                    }else{
                        ToastUtil.showSingletonShort("挑战金额加载失败");
                    }
                }
                break;
            case R.id.tv_zj:
                UserInfo user3 = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
                if(user3==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }else{
                    if(homeDialog!=null){
                        homeDialog.show();
                    }else{
                        ToastUtil.showSingletonShort("挑战金额加载失败");
                    }
                }
                break;
            case R.id.tv_tzgz:
                if(noticeDialog==null){
                   getTzRule();
                }else{
                    noticeDialog.show();
                }
                break;
            case R.id.tv_gg:
                if(msgDialog!=null){
                    msgDialog.show();
                }else{
                    getMsgRule();
                }
                break;
            case R.id.rl_right:
                UserInfo user2 = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
                if(user2==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }else{
                    startActivity(new Intent(getActivity(), MsgListActivity.class));
                }
                break;
            case R.id.tv_dk:
                UserInfo user4 = SharedPreferencesUtil.getInstance().getObject("user", UserInfo.class);
                if(user4==null){
//                    Toast.makeText(getActivity(),"请先登录",Toast.LENGTH_SHORT).show();
                    ToastUtil.showSingletonShort("请先登录");
                }else{
//                    dk();
                    startActivity(new Intent(getActivity(), CodeActivity.class));
                }
                break;
            case R.id.tv_djs:
//                PayStatusDialog payStatusDialog = new PayStatusDialog(getActivity(),"下次打卡时间为\n2018年",1);
//                payStatusDialog.show();
                ToastUtil.showSingletonShort("未到打卡时间");
                break;
        }
    }
    /**
     * 查询打卡
     */
    public void showDkDialog(){
        ViseHttp.GET(UrlDefend.PAY_STATUS)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject object = new JSONObject(data);
                            int type = object.getInt("type");
                            String content = object.getString("content");
                            String contentTime = object.getString("contentTime");
                                PayStatusDialog payStatusDialog = new PayStatusDialog(getActivity(),content,contentTime,type);
                                payStatusDialog.show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }
    /**
     * 打卡
     */
    private void dk() {
        ViseHttp.POST(UrlDefend.DAKA)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        ToastUtil.showSingletonShort("打卡成功!");
                        setStatus(2);
                        refreshLayout.autoRefresh();
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        ToastUtil.showSingletonShort(errMsg);
                    }
                });
    }

    public void getTzRule(){
        PublicUtil.getRule(Constants.RULE_TZ, new ACallback<Rule>() {
            @Override
            public void onSuccess(Rule data) {
                noticeDialog = new NoticeDialog(getActivity(),data,"确定");
                noticeDialog.show();
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                ToastUtil.showSingletonShort(errMsg);
            }
        });
    }

    public void getMsgRule(){
        //获取公告
        PublicUtil.getRule(Constants.RULE_HOME, new ACallback<Rule>() {
            @Override
            public void onSuccess(Rule data) {
                msgDialog = new NoticeDialog(getActivity(),data,"朕知道了");
                msgDialog.show();
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                ToastUtil.showSingletonShort(errMsg);
            }
        });
    }
}
